import unittest
import SQLGenerator
import SyntaxParser
import inspect
from definitions import *

expectedResults = []
expectedResults.append("SELECT D_Patients.mrn, D_MedItems.label, MedEvents.dose, MedEvents.charttime FROM D_MedItems, MedEvents, D_Patients WHERE MedEvents.itemid = D_MedItems.itemid AND MedEvents.pid = D_Patients.pid AND MedEvents.charttime >= to_date('2007-11-13-06','YYYY-MM-DD-HH24') AND MedEvents.charttime < to_date('2007-11-19-14','YYYY-MM-DD-HH24') AND lower(D_MedItems.label) IN ('fentanyl','ketamine','midazolam','morphine','propofol','remifentanil','thiopentone','tramadol') AND medevents.cuid = 4 ORDER BY D_Patients.mrn, D_MedItems.label, MedEvents.charttime")
expectedResults.append("SELECT D_Patients.mrn FROM D_Patients")
expectedResults.append("SELECT D_Patients.mrn FROM D_Patients where D_Patients.sex = male")

class SQLGeneratorTest(unittest.TestCase):
	def setUp(self):
		self.groupevents_dictionary = SyntaxParser.buildHash(groupevents_dictionary_path)
		self.archive_labevents_dictionary = SyntaxParser.buildHash(archive_labevents_dictionary_path)
		self.archive_medevents_dictionary = SyntaxParser.buildHash(archive_medevents_dictionary_path)
		self.archive_chartevents_dictionary = SyntaxParser.buildHash(archive_chartevents_dictionary_path)
		self.archive_patientevents_dictionary = SyntaxParser.buildHash(archive_patientevents_dictionary_path)

	def testSimpleQuery(self):
		self.generatePreconditions("all values of sedation", "", "", 200, "hours", "GICU")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		sqlString = sql.generateArchiveSQL("groupevents", 0)
		
		# *** Expected result ***
		expectedString = "SELECT D_Patients.mrn, D_MedItems.label, MedEvents.dose, MedEvents.charttime FROM D_MedItems, MedEvents, D_Patients WHERE MedEvents.itemid = D_MedItems.itemid AND MedEvents.pid = D_Patients.pid AND MedEvents.charttime >= to_date('2007-11-13-06','YYYY-MM-DD-HH24') AND MedEvents.charttime < to_date('2007-11-19-14','YYYY-MM-DD-HH24') AND lower(D_MedItems.label) IN ('fentanyl','ketamine','midazolam','morphine','propofol','remifentanil','thiopentone','tramadol') AND medevents.cuid = 4 ORDER BY D_Patients.mrn, D_MedItems.label, MedEvents.charttime"
		
		self.assertEquals(sqlString, expectedString)

	def testAllPatients(self):
		self.generatePreconditions("all values of mrn", "", "", 0, "", "")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing all patients"
		sqlString = sql.generateArchiveSQL("patientevents", 0)
		print sqlString, "\n"

	def testAllPatientsOver50(self):
		self.generatePreconditions("all values of mrn", "", "age > 50", 0, "", "")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing all patients over 50"
		sqlString = sql.generateArchiveSQL("patientevents", 0)
		print sqlString, "\n"

	def testAllPatientsOver50AndLessThan70(self):
		self.generatePreconditions("all values of mrn", "", "age > 50 and age < 70", 0, "", "")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing all patients over 50 and less than 70"
		sqlString = sql.generateArchiveSQL("patientevents", 0)
		print sqlString, "\n"

	def testAllPatientsLessThan50_orOver70(self):
		self.generatePreconditions("all values of mrn", "", "age < 50 or age > 70", 0, "", "")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing all patients less than 50 or over 70"
		sqlString = sql.generateArchiveSQL("patientevents", 0)
		print sqlString, "\n"

	def testMalePatients(self):
		self.generatePreconditions("all values of mrn", "", "sex = male", 0, "", "")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing male patients"
		sqlString = sql.generateArchiveSQL("patientevents", 0)
		print sqlString, "\n"

	def testMaleAndFemalePatients(self):
		self.generatePreconditions("all values of mrn", "", "sex = male and sex = female", 0, "", "")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing male and female patients"
		sqlString = sql.generateArchiveSQL("patientevents", 0)
		print sqlString, "\n"
	
	def testAllInLastHour(self):
		self.generatePreconditions("all values of mrn", "", "", 1, "hours", "GUCU")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing all last hour patients"
		sqlString = sql.generateArchiveSQL("patientevents", 0)
		print sqlString, "\n"

	def testAllInLast0HoursMale(self):
		self.generatePreconditions("all values of mrn", "", "sex = male", 0, "hours", "GUCU")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing all last 0 hour male patients"
		sqlString = sql.generateArchiveSQL("patientevents", 0)
		print sqlString, "\n"

	def testAllInLastMinus1HoursPh(self):
		self.generatePreconditions("all values of ph", "", "", -1, "hours", "GUCU")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing all last -1 hour male patients and getting values of ph"
		sqlString = sql.generateArchiveSQL("labevents", 0)
		print sqlString, "\n"

	def testAllInLast10DaysPh(self):
		self.generatePreconditions("all values of ph", "", "", 10, "days", "GUCU")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing all last 10 day patients and getting values of ph"
		sqlString = sql.generateArchiveSQL("labevents", 0)
		print sqlString, "\n"

	def testAllInLast0DaysPh(self):
		self.generatePreconditions("all values of ph", "", "", 0, "days", "GUCU")
		self.generateAnswers()
		sql = SQLGenerator.SQLGenerator("SNOMED", "GICU", self.groupevents_answers, self.patientevents_answers,\
		self.chartevents_answers, self.medevents_answers, self.labevents_answers, self.patientevents_conditions,\
		self.chartevents_conditions, self.medevents_conditions, self.labevents_conditions, None, self.time_val, self.time_ent, self.ward, None)
		
		# *** Actual result ***
		print "Testing all last 0 day patients and getting values of ph"
		sqlString = sql.generateArchiveSQL("labevents", 0)
		print sqlString, "\n"

	def tearDown(self):
		pass

	def generatePreconditions(self, answer, cond, patient, time_val, time_ent, ward):
		self.answer = answer
		self.cond = cond
		self.patient = patient
		self.time_val = time_val
		self.time_ent = time_ent
		self.ward = ward
		self.duration = SyntaxParser.toHours(self.time_val, self.time_ent)
	
	def generateAnswers(self):
		self.groupevents_answers = SyntaxParser.getAnswers(self.answer, ref_fns, stat_fns, self.groupevents_dictionary)
		self.chartevents_answers = SyntaxParser.getAnswers(self.answer, ref_fns, stat_fns, self.archive_chartevents_dictionary)
		self.patientevents_answers = SyntaxParser.getAnswers(self.answer, ref_fns, stat_fns, self.archive_patientevents_dictionary)
		self.medevents_answers = SyntaxParser.getAnswers(self.answer, ref_fns, stat_fns, self.archive_medevents_dictionary)
		self.labevents_answers = SyntaxParser.getAnswers(self.answer, ref_fns, stat_fns, self.archive_labevents_dictionary)
		self.patient_conditions = SyntaxParser.getConditions(self.patient, comparands, types, self.archive_patientevents_dictionary, 'T')
		self.patientevents_conditions = SyntaxParser.transform_Conditions(self.patient_conditions, "archive")
		self.chartevents_conditions = SyntaxParser.getConditions(self.cond, comparands, types, self.archive_chartevents_dictionary)
		self.medevents_conditions = SyntaxParser.getConditions(self.cond, comparands, types, self.archive_medevents_dictionary)
		self.labevents_conditions = SyntaxParser.getConditions(self.cond, comparands, types, self.archive_labevents_dictionary)

if __name__ == "__main__":
		unittest.main()
		
